const { chromium } = require('playwright');

async function captureScreenshot(url, outputPath) {
  console.log('启动浏览器...');
  const browser = await chromium.launch({
    headless: false
  });
  
  const context = await browser.newContext({
    viewport: { width: 1920, height: 1080 }
  });
  
  const page = await context.newPage();
  
  console.log(`导航到: ${url}`);
  await page.goto(url, { 
    waitUntil: 'networkidle',
    timeout: 30000 
  });
  
  console.log('等待页面加载完成...');
  await page.waitForTimeout(3000);
  
  console.log(`截图保存到: ${outputPath}`);
  await page.screenshot({ 
    path: outputPath,
    fullPage: true 
  });
  
  console.log('关闭浏览器...');
  await browser.close();
  
  console.log('✅ 截图完成！');
}

const url = process.argv[2] || 'https://www.example.com';
const outputPath = process.argv[3] || `screenshot_${Date.now()}.png`;

captureScreenshot(url, outputPath).catch(console.error);